var box = document.getElementById('drop_area'); //拖拽区域
var dropFile;
jcUtils.enableDropEvent(function (files) {

  let file = files[0];
  if (file.type !== "application/zip" && file.type !== "application/x-zip-compressed") {
    alert('请选择正确的zip类型文件')
    return;
  }

  var str = "文件名字:" + file.name;
  document.getElementById('drop_area').innerHTML = str;

  dropFile = file;
}, box)

$("#btn_upload").click(() => {
  if (dropFile == null) {
    alert("上传文件不能为空");
    return;
  }
  console.log('click Upload... ...');

  var formData = new FormData();
  formData.append('file', dropFile);
  let data = jcUtils.GetSecret();
  formData.append('ts', data[0]);
  formData.append('secret', data[1]);

  $.ajax({
    url: 'http://www.geek7.ltd:8081/BuildPlayable/',
    type: 'POST',
    data: formData,
    mimeType: "multipart/form-data",
    cache: false,
    processData: false,
    contentType: false,
    responseType: 'arraybuffer',
    success: function (data) {

      try {

        let r = JSON.parse(data);
        if (r.succeed) {
          
          let url = r.url;
          (function (url) {
            let name = url.substring(url.lastIndexOf("/") + 1);
            let objectURL = window.URL.createObjectURL(new Blob([url]));
            let a = document.createElement('a')
            a.href = objectURL
            a.download = name
            a.click()
            a.remove()
          })(url)

        }

      }
      catch (e) {
        console.log(e);
      }
    },
  });

});